package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.TeacherDao;
import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherService {
    private TeacherDao teacherDao = new TeacherDao();

    //添加老师
    public boolean addTeacher(Teacher teacher) {
        //返回库管拿到的结果传给客服
        return teacherDao.addTeacher(teacher);
    }

    //判断工号是否唯一
    public boolean isExists(String id) {
        //通过库管拿到所有老师信息
        Teacher[] ter = teacherDao.findAllTeacher();
        //定义标记位假设不存在
        boolean flag = false;
        for (int i = 0; i < ter.length; i++) {
            Teacher teachers = ter[i];
            //如果库管的对象不为空并且跟传入的工号一样表示存在了
            if (teachers != null && teachers.getSid().equals(id)) {
                flag = true;
            }
        }
        return flag;
    }

    //拿到库管中所有信息
    public Teacher[] findAllTeacher() {

        Teacher[] ter = teacherDao.findAllTeacher();
        //标记位,假设没有信息为false
        boolean flag = false;
        //遍历老师数组
        for (int i = 0; i < ter.length; i++) {
            Teacher teachers = ter[i];
            //如果对象不为空表示数组里有信息,标记位true
            if (teachers != null) {
                flag = true;
                break;
            }
        }
        //判断,如果true有信息就返回这个数组,没有就返回null
        if (flag) {
            return ter;
        } else {
            return null;
        }
    }

    public void deletTeachuer(String id) {
        teacherDao.deletTeacher(id);
    }
//调用库管的修改方法 传入对象和修改的id,返回一个结果
    public void updateTeacher(String updateId,Teacher teacher) {
        teacherDao.updateTeacher(updateId,teacher);
    }
}
